package com.ruoyi.mk.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.framework.config.ServerConfig;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mk.domain.MkTariff;
import com.ruoyi.mk.service.IMkTariffService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * mk_tariffController
 * 
 * @author ruoyi
 * @date 2024-04-02
 */
@RestController
@RequestMapping("/tool/tariff")
public class MkTariffController extends BaseController
{
    @Autowired
    private IMkTariffService mkTariffService;

    @Autowired
    private ServerConfig serverConfig;

    /**
     * 查询mk_tariff列表
     */
    @GetMapping("/list")
    @Anonymous
    public TableDataInfo list(MkTariff mkTariff)
    {
        startPage();
        List<MkTariff> list = mkTariffService.selectMkTariffList(mkTariff);
        return getDataTable(list);
    }

    @GetMapping("/search")
    @Anonymous
    public TableDataInfo search(MkTariff mkTariff)
    {
        List<MkTariff> list = mkTariffService.selectMkTariffList(mkTariff);
        return getDataTable(list);
    }

    /**
     * 导出mk_tariff列表
     */
    @PreAuthorize("@ss.hasPermi('system:tariff:export')")
    @Log(title = "mk_tariff", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MkTariff mkTariff)
    {
        List<MkTariff> list = mkTariffService.selectMkTariffList(mkTariff);
        ExcelUtil<MkTariff> util = new ExcelUtil<MkTariff>(MkTariff.class);
        util.exportExcel(response, list, "mk_tariff数据");
    }

    /**
     * 获取mk_tariff详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:tariff:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(mkTariffService.selectMkTariffById(id));
    }

    /**
     * 新增mk_tariff
     */
    @PreAuthorize("@ss.hasPermi('system:tariff:add')")
    @Log(title = "mk_tariff", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody MkTariff mkTariff)
    {
        return toAjax(mkTariffService.insertMkTariff(mkTariff));
    }

    /**
     * 修改mk_tariff
     */
    @PreAuthorize("@ss.hasPermi('system:tariff:edit')")
    @Log(title = "mk_tariff", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody MkTariff mkTariff)
    {
        return toAjax(mkTariffService.updateMkTariff(mkTariff));
    }

    /**
     * 删除mk_tariff
     */
    @PreAuthorize("@ss.hasPermi('system:tariff:remove')")
    @Log(title = "mk_tariff", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mkTariffService.deleteMkTariffByIds(ids));
    }


    /**
     * 下载导入模版
     * */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<MkTariff> util = new ExcelUtil<MkTariff>(MkTariff.class);
        util.importTemplateExcel(response, "关税表");
    }

    /**
     * 导入mk_tariff
     */
    @PreAuthorize("@ss.hasPermi('system:tariff:import')")
    @Log(title = "mk_tariff", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws IOException {
        String filePath = RuoYiConfig.getUploadPath();

        ExcelUtil<MkTariff> util = new ExcelUtil<MkTariff>(MkTariff.class);
        List<MkTariff> List = util.importExcel(file.getInputStream());
        for (MkTariff mkTariff : List){
            String pictureUrl = serverConfig.getUrl() + mkTariff.getPicture();
            mkTariff.setPicture(pictureUrl);
            mkTariffService.insertMkTariff(mkTariff);
        }
        return AjaxResult.success();
    }

    /**
     * 删除图片
     */
    @PreAuthorize("@ss.hasPermi('system:tariff:remove')")
    @Log(title = "mk_tariff", businessType = BusinessType.DELETE)
    @DeleteMapping("/imageremove")
    public boolean  imageremove(@RequestBody String path)
    {
        return mkTariffService.imageremove(path);
    }
}
